//
//  SearchDataViewController.m
//  NewMyMusicAPP
//
//  Created by 08- 张志强 on 15/6/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SearchDataViewController.h"

@interface SearchDataViewController ()
@property(nonatomic,retain)MBProgressHUD *MBP;
@end

@implementation SearchDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor lightGrayColor];
    
  //  NSLog(@"searchDA.TITLE ===== %@",self.title);
    
    self.myTableView=[[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667 - 64) style:UITableViewStylePlain]autorelease];
    self.myTableView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    
    self.myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;//cell隐藏线
    
    //返回按钮
    UIImage *image2=[UIImage imageNamed:@"VList_Left@2x.png"];
    image2=[image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(PopButton)];
    
    
    //菊花
    //菊花(小黄人)
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    self.array=[NSMutableArray array];
    
    [self searchDatashow];
    self.isUpLoading = NO;
    [self addFooter:1];
    [self addHeader];


      [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
}

-(void)addHeader
{
    __block SearchDataViewController *searchDV=self;
    
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [searchDV searchDatashow];
        
    }];
    
    //一进程序自动刷新
    //[self.myTableView.header beginRefreshing];
}

//上啦加载
-(void)addFooter:(NSInteger)number
{
    __block SearchDataViewController *premierVC=self;
    self.myTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        premierVC.isUpLoading=YES;
        
        
        _pageNumber = _pageNumber + 20;
        NSString *newPage = [NSString stringWithFormat:@"%d",_pageNumber ];
       // NSLog(@"newPage = %@",newPage);
        // _preDic = [NSMutableDictionary dictionary];//接口字典
        
        //[self.MBP show:YES];
        [self searchDatashow];
        [_preDic setObject:newPage forKey:@"offset"];
        
        premierVC.nextPage ++;
        //[premierVC searchDatashow];
        
        
    }];
    
}

//返回
-(void)PopButton
{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCell=@"cell";
    SearchDataViewCell *cell=[tableView dequeueReusableCellWithIdentifier:myCell];
    if (cell == nil) {
        cell = [[[SearchDataViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell]autorelease];
        
        
    }
    cell.backgroundColor=[UIColor blackColor];
    
//    if (self.array.count != 0) {
//        cell.searchModel = [self.array objectAtIndex:indexPath.row];
//    }
  
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];
    
    
    //收藏
    if (self.array.count != 0) {
        SearchDataModel *searchModel = [self.array objectAtIndex:indexPath.row];
        cell.searchModel = [self.array objectAtIndex:indexPath.row];
        
        [[DataHandle shareHandleController] openDB];
        [[DataHandle shareHandleController] createCollectTable];
        
        NSMutableArray *array = [[DataHandle shareHandleController]selectCollect];
        if (array.count == 0) {
            cell.buer=NO;
        }else
        {
            for (CollectModel *collect in array) {
                NSString *modeID= [NSString stringWithFormat:@"%@",searchModel.idd];
                if ([collect.modelId isEqualToString:modeID]) {
                    cell.buer=YES;
                    cell.searchModel=[self.array objectAtIndex:indexPath.row];
                    return cell;
                }
            }
            cell.buer=NO;
            cell.searchModel=[self.array objectAtIndex:indexPath.row];
        }
    }

    
    
    
    return cell;

    
}
//多少cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;
    }else
    {
        return 0;
    }
    
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 110;
    
}
//点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([DataHandle shareHandleController].monitorNetwork==NO) {
        
        UIAlertView *alertView =[ [UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [GiFHUD dismiss];
        
    }else
    {
    
    
    VideoPlayerViewController *videoVC=[[[VideoPlayerViewController alloc]init]autorelease];
    
    if (self.array.count != 0) {
        SearchDataModel *searchModel=[self.array objectAtIndex:indexPath.row];
        videoVC.Id=searchModel.idd;
        videoVC.type=@"VIDEO";
        
        [self.navigationController pushViewController:videoVC animated:YES];
    }
    
    
    }
}

//请求数据
-(void)searchDatashow
{
    NSString *data=self.title;
    
    NSString *dataUTF8=[data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSMutableDictionary *searDic=[NSMutableDictionary dictionary];
    [searDic setObject:@"0" forKey:@"D-A"];
    [searDic setObject:dataUTF8 forKey:@"keyword"];
    [searDic setObject:[NSString stringWithFormat:@"%ld", _pageNumber] forKey:@"offset"];
    [searDic setObject:@"20" forKey:@"size"];
  //  D-A=0&keyword=%E8%94%A1%E4%BE%9D%E6%9E%97&offset=0&size=20
    //D-A=0&keyword=T-ARA                      &offset=0&size=20
    
   
    
    [Connect ConnectRequestAFWithURL:SearchMV params:searDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
        
        NSMutableArray *myArray=[(NSMutableDictionary *)result objectForKey:@"videos"];
        
        if ([DataHandle shareHandleController].monitorNetwork==NO) {
            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [aler show];
            [GiFHUD dismiss];
            
        }else
        {
        
        
        
        //NSLog(@"result ========== %@",result);
        for (NSMutableDictionary *dic in myArray) {
            SearchDataModel *searDataModel=[[[SearchDataModel alloc]init]autorelease];
            searDataModel.idd=[dic objectForKey:@"id"];
            //searDataModel.type=[dic objectForKey:@"type"];
            searDataModel.title=[dic objectForKey:@"title"];
            searDataModel.artistName=[dic objectForKey:@"artistName"];
            searDataModel.albumImg=[dic objectForKey:@"albumImg"];
            searDataModel.posterPic = [dic objectForKey:@"posterPic"];
            [self.array addObject:searDataModel];
            
        
          //  NSLog(@"hhhhhhhh%@",searDataModel.title);
        }
        if (self.array.count==0) {
            UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有查到该艺人相关信息呦~~" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [GiFHUD dismiss];//动态图隐藏
            [alertV show];
        }else
        {
        
        [GiFHUD dismiss];//动态图隐藏
        [self.myTableView.header endRefreshing];
        [self.myTableView.footer endRefreshing];
        [self.myTableView reloadData];
        }
        }
    }];
    
  
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
